package com.qf.service.impl;

import cn.hutool.core.util.ReUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.mapper.RecycleMapper;
import com.qf.myexception.ServiceException;
import com.qf.pojo.Recycle;
import com.qf.service.RecycleService;
import com.qf.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.file.NotLinkException;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.Map;

@Service
public class RecycleServiceImpl extends ServiceImpl<RecycleMapper, Recycle> implements RecycleService{

    @Autowired
    RecycleMapper recycleMapper;


    @Override
    public ResultMsg findAll(int current, int pageSize) {
        QueryWrapper<Recycle> wrapper = new QueryWrapper<>();
        wrapper.eq("recycle_delete",0);
        Page<Recycle> page = new Page<>(current, pageSize);
        Page<Recycle> page1 = recycleMapper.getPage(page, wrapper);
        if(page1!=null){
            return ResultMsg.SUCCESS(page1);
        }
        throw new ServiceException(201,"查询失败");
    }

    @Override
    public ResultMsg findOne(int recycleId) {
        QueryWrapper<Recycle> wrapper = new QueryWrapper<>();
        wrapper.eq("recycle_id",recycleId);
        Recycle one = recycleMapper.findOne(wrapper);
        if(one!= null)
            return ResultMsg.SUCCESS(one);
        throw new ServiceException(201,"查询失败");
    }

    @Override
    public ResultMsg findAllByDate(String startTime, String endTime, Integer current, Integer pageSize) {
        QueryWrapper<Recycle> wrapper = new QueryWrapper<>();
        wrapper.eq("recycle_delete",0);
        wrapper.between("recycle_date",startTime,endTime);
        Page<Recycle> page = new Page<>(current, pageSize);
        Page<Recycle> page1 = recycleMapper.getPage(page, wrapper);
        if(page1!=null){
            return ResultMsg.SUCCESS(page1);
        }
        throw new ServiceException(201,"查询失败");
    }

    @Override
    public ResultMsg add(Recycle recycle) {
        recycle.setRecycleDelete(0);
        recycle.setRecycleId(null);
        //recycle.setRecycleDate(new Timestamp(new java.util.Date().getTime()));
        boolean save = this.save(recycle);
        if(save){
            return ResultMsg.SUCCESS();
        }
        throw new ServiceException(201,"新增失败");
    }

    @Override
    public ResultMsg updateRecycle(Recycle recycle) {
        if(recycle.getRecycleId()==null){
            throw new ServiceException(201,"recycleId参数缺少");
        }
        boolean b = this.updateById(recycle);
        if(b){
            return ResultMsg.SUCCESS();
        }
        throw new ServiceException(201,"修改失败");
    }

    @Override
    public ResultMsg delRecycle(Map<String,Object> map) {
        UpdateWrapper<Recycle> wrapper = new UpdateWrapper<>();
        wrapper.set("recycle_delete",1);
        Integer recycleId = (Integer) map.get("recycleId");
        wrapper.eq("recycle_id",recycleId);
        boolean update = this.update(wrapper);
        if(update){
            return ResultMsg.SUCCESS();
        }
        throw new ServiceException(201,"删除失败");
    }
}
